/**
 * Project Name:JavaBaseReview
 * Package Name:com.xuwei.ext.netty.example
 * author:David
 * Date:2016年6月17日上午9:33:03
 * Copyright (c) 2016, xw123box@126.com All Rights Reserved.
 *
 */
package com.xuwei.ext.netty.example;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author David
 * @since 2016年6月17日 上午9:33:03
 * @version 
 * @since JDK 1.6
 */
public class TimeClient {
	
	public static void main(String[] args) throws Exception {
		String host = args[0];
		int port = Integer.parseInt(args[1]);
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try{
			Bootstrap b = new Bootstrap();
			b.group(workerGroup);
			b.channel(NioSocketChannel.class);
			b.option(ChannelOption.SO_KEEPALIVE, true);
			b.handler(new ChannelInitializer<SocketChannel>() {
				@Override
				protected void initChannel(SocketChannel ch)
						throws Exception {
					ch.pipeline().addLast(new TimeClientHandler());
				}
			});
			//start the client
			ChannelFuture f = b.connect(host, port).sync();
			//wait until the connetion is closed.
			f.channel().closeFuture().sync();
		}finally{
			workerGroup.shutdownGracefully();
		}
	}
}
